package vn.gq.tym.view;

import vn.gq.tym.model.ChapterAdapter;
import vn.gq.tym.model.bean.CategoryItem;
import vn.gq.tym.truyennguoilonfull.Configuation;
import vn.gq.tym.truyennguoilonfull.R;
import vn.gq.tym.truyennguoilonfull.ReaderActivity;
import vn.gq.tym.utils.IClientListener;
import vn.gq.tym.utils.OnNetworkListener;
import vn.gq.tym.widget.BaseFragment;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;

public class ChapterFragment extends BaseFragment implements OnItemClickListener, OnNetworkListener, OnClickListener {

	private ListView mListView;
	private ProgressBar mProgressBar;
	private ChapterAdapter mAdapter;
	private CategoryItem mCategoryItem;
	private View mBounder;
	private Button mBtnRetry;
	
	public static ChapterFragment newInstance(CategoryItem catItem) {
		ChapterFragment f = new ChapterFragment();
		Bundle args = new Bundle();
		args.putParcelable("CATEGORY", catItem);
		f.setArguments(args);
		return f;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getArguments() != null) {
			mCategoryItem = getArguments().getParcelable("CATEGORY");
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if (!mCategoryItem.getTitle().equals(""))
			getSherlockActivity().getSupportActionBar().setSubtitle(mCategoryItem.getTitle());
	}
	
	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public int getLayoutId() {
		return R.layout.fragment_chapter;
	}

	@Override
	public int getContainerId() {
		return R.id.layout_container;
	}

	@Override
	public void createView(Bundle savedInstanceState) {
		mListView = (ListView) findViewById(R.id.list);
		mListView.setOnItemClickListener(this);
		mListView.setVisibility(View.GONE);
		mBounder = findViewById(R.id.nonetwork);
		mBtnRetry = (Button) findViewById(R.id.btn_reconnect);
		mBtnRetry.setOnClickListener(this);
		mProgressBar = (ProgressBar) findViewById(R.id.loading);
		mAdapter = new ChapterAdapter(getActivity(), this);
		load();
	}
	
	public void load() {
		mProgressBar.setVisibility(View.VISIBLE);
		mAdapter.load(mCategoryItem.getUrl(), new Runnable() {
			
			@SuppressWarnings("unused")
			@Override
			public void run() {
				mListView.setAdapter(mAdapter);
				mListView.setVisibility(View.VISIBLE);
				mProgressBar.setVisibility(View.GONE);
				if (getActivity() instanceof IClientListener && !Configuation.MULTI_STORIES_APP) {
					((IClientListener) getActivity()).onDone(0, null);
				}
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (id > -1) {
			ReaderActivity.startActivity(getActivity(), mCategoryItem, mAdapter.getData(), position, 0);
		}
	}

	@Override
	public void onNoNetwork() {
		mBounder.setVisibility(View.VISIBLE);
	}

	@Override
	public void onClick(View v) {
		mBounder.setVisibility(View.GONE);
		load();
	}
}
